package com.ruoyi.video.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.video.domain.Video;
import com.ruoyi.video.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 番剧Controller
 *
 * @author ruoyi
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/video/video")
public class VideoController extends BaseController {
    @Autowired
    private IVideoService videoService;

    /**
     * 查询番剧列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(Video video) {
        startPage();
        List<Video> list = videoService.selectVideoList(video);
        return getDataTable(list);
    }


    /**
     * 查询历史观看番剧列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:list')")
    @GetMapping("/getHistoryVideoList")
    public TableDataInfo getHistoryVideoList() {
        startPage();
        List<Video> list = videoService.selectHistoryVideoList();
        return getDataTable(list);
    }


    /**
     * 导出番剧列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:export')")
    @Log(title = "番剧", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Video video) {
        List<Video> list = videoService.selectVideoList(video);
        ExcelUtil<Video> util = new ExcelUtil<Video>(Video.class);
        util.exportExcel(response, list, "番剧数据");
    }

    /**
     * 获取番剧详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(videoService.selectVideoById(id));
    }

    /**
     * 新增番剧
     */
    @PreAuthorize("@ss.hasPermi('video:video:add')")
    @Log(title = "番剧", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Video video) {
        if (StringUtils.isEmpty(video.getId())) video.setId(IdUtils.fastUUID());
        return toAjax(videoService.insertVideo(video));
    }

    /**
     * 修改番剧
     */
    @PreAuthorize("@ss.hasPermi('video:video:edit')")
    @Log(title = "番剧", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Video video) {
        return toAjax(videoService.updateVideo(video));
    }

    /**
     * 刷新视频列表
     */
    @PreAuthorize("@ss.hasPermi('video:video:edit')")
    @Log(title = "番剧", businessType = BusinessType.UPDATE)
    @PostMapping("/syncVideoFolder")
    public AjaxResult syncVideoFolder() {
//        return AjaxResult.error("接口已停用");
        return AjaxResult.success(videoService.syncVideoFolder());
    }

    /**
     * 删除番剧
     */
    @PreAuthorize("@ss.hasPermi('video:video:remove')")
    @Log(title = "番剧", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(videoService.deleteVideoByIds(ids));
    }


    /**
     * 番剧头图上传
     */
    @Log(title = "番剧头图上传", businessType = BusinessType.UPDATE)
    @PostMapping("/imgUpload")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            try {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, new String[]{"jpg", "png", "gif"});
                return AjaxResult.success(avatar);
            } catch (InvalidExtensionException e) {
                logger.error("番剧头图上传出错", e);
            }
        }
        return AjaxResult.error("上传图片异常");
    }

}
